
const Article = require('../models/article')
class ArticleCtl {
  async find(ctx) {
    const id = ctx.user._id;
    const acticle = await Article.find({authorId: id});
    ctx.body = {
      success: true,
      data: acticle
    }
  }
  async findById(ctx) {
    try {
      const acticle = await Article.findById(ctx.params.id);
      ctx.body = {
        success: true,
        message: '查询成功',
        data: acticle
      }
    } catch (error) {
      ctx.body = {
        success: false,
        message: '查询失败',
      }
    }

  }
  async findAll(ctx) {
    // 获取请求参数中的页码和每页显示的数量
    const page = parseInt(ctx.query.pageNo || '1', 10); // 默认为第一页
    const limit = parseInt(ctx.query.pageSize || '10', 10); // 默认每页显示 10 条

    // 计算需要跳过的文档数量
    const skip = (page - 1) * limit;

    try {
        // 查询总条数
        const total = await Article.find().countDocuments();

        // 使用 skip 和 limit 进行分页查询
        const articles = await Article.find().skip(skip).limit(limit);

        // 返回结果
        ctx.body = {
            success: true,
            data: articles,
            pageNo: page,
            pageSize: limit,
            totalCount: total
        };
    } catch (error) {
        // 处理查询错误
        ctx.body = {
            success: false,
            message: 'Failed to fetch articles',
            error: error.message
        };
    }
}
  async create(ctx) {

    const {title, content} = ctx.request.body;

    console.log('ctx', ctx.user);
    
    const article = {
      title,
      content,
      authorId: ctx.user._id,
      authorName: ctx.user.user,
      createdAt: Date.now()
    }
    console.log('article', article);
    try {
      const data = new Article(article);
      await data.save();
      ctx.body = {
        success: true,
        message: '创建成功',
        data: data
      }
      
    } catch (error) {
      console.log('error',error);
      ctx.status = 500
      ctx.body = {
        success: false,
        message: '创建失败',
      }
    }
    

  }
  async updata(ctx) {

    const {title, content} = ctx.request.body;

    console.log('ctx', ctx.user);
    
    const article = {
      title,
      content,
      createdAt: Date.now()
    }

    try {
      const data = await Article.findByIdAndUpdate(ctx.params.id, article);
      ctx.body = {
        success: true,
        message: '修改成功',
        data: data
      }
      
    } catch (error) {
      console.log('error',error);
      ctx.status = 500
      ctx.body = {
        success: false,
        message: '修改失败',
      }
    }
    

  }

  async delete(ctx) {
    try {
      const article = await Article.findByIdAndDelete(ctx.params.id);
      if (!article) {
        ctx.body = {
          success: false,
          message: '未找到指定用户',
        };
      } else {
        ctx.body = {
          success: true,
          data: article,
          message: '删除成功',
        };
      }
    } catch (error) {
      console.error('Error deleting user:', error); // 输出错误信息
      ctx.body = {
        success: false,
        message: '删除失败',
      };
    }
  }
}

module.exports = new ArticleCtl()
